﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;
using Xiaov.SmartWeb.Model.ActiveRecord;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.SysAdmin
{
    public partial class Admin_Dept : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Xiaov.SmartWeb.Application.SysAdmin.Admin_Dept));
            PM_Filter();            
        }

        [AjaxMethod]
        public bool Save(object id, object name, object parentid, object disable, object sortby)
        {
            bool rtn = false;
            try
            {
                Xiaov.SmartWeb.Model.ActiveRecord.DWBMB model = new DWBMB();
                model = DWBMB.TryFind(id);
                if (model == null)
                {
                    model = new DWBMB();
                    model.DWBM = id.ToString();                    
                }
                model.DWMC = name.ToString();
                //model.PARENTID = string.IsNullOrEmpty(parentid.ToRequestString())?"0":parentid.ToRequestString();
                if (disable == null)
                    disable = "false";
                model.DISABLED = (disable.ToString() == "false" ? "0" : "1");

                model.XSSX = sortby.ToInt32();
                model.Save();
                model.RefreshCache();
                rtn = true;
            }
            catch
            {
                rtn = false;
            }
            return rtn;
        }

        [AjaxMethod]
        public bool DeleteNode(object nodeid)
        {
            bool rtn = false;
            try
            {
                DWBMB model = DWBMB.Find(nodeid);
                DWBMB.DeleteAll("DWBM='" + nodeid + "'");
                model.Delete();
                model.RefreshCache();
                rtn = true;
            }
            catch
            {
                rtn = false;
            }
            return rtn;
        }

        [AjaxMethod]
        public bool ClearNode(object nodeid)
        {
            bool rtn = false;
            try
            {
                DWBMB model = new DWBMB();
                DWBMB.DeleteAll("DWBM='" + nodeid + "' or PARENTID='" + nodeid + "'");
                model.RefreshCache();
                rtn = true;
            }
            catch
            {
                rtn = false;
            }
            return rtn;
        }

        [AjaxMethod]
        public string MergeChildNode(string dwbm1,string dwbm2)
        {
            if (dwbm1.ToString().Length != 8 || dwbm2.ToString().Length != 8)
            {
                throw new Exception("请选择两个四级单位进行操作！");                
            }
            else
            {
                if (dwbm1 == dwbm2)
                {
                    throw new Exception("相同的不能合并");
                }
                else
                {
                    string updateSql = "begin Delete from DWBMB t where t.DWBM='" + dwbm1 + "';Update  DJJCXX t set t.DWBM='" + dwbm2.ToString() + "' where t.DWBM='" + dwbm1.ToString() + "'; end;";
                    int i = Maticsoft.DBUtility.DbHelperOra.ExecuteSql(updateSql);
                    if (i > 0)
                    {
                        DWBMB dwbmb = new DWBMB();
                        dwbmb.RefreshCache();
                        return "删除" + dwbm1.ToString() + "成功!,并将其所有单井转入" + dwbm2.ToString() + "!";
                    }
                    else
                    {
                        return "没有影响任何数据";
                    }
                }
                
            }
            
 
        }

        [AjaxMethod]
        public string MoveChildNode(string dwbm1,string dwbm2)
        {
            if (dwbm1.ToString().Length != 8 || dwbm2.ToString().Length != 6)
            {
                throw new Exception("第一个请选择四级，第2个请选择三级进行操作！");                
            }
            else
            {
                if (dwbm1.ToRequestString().Substring(0,6) == dwbm2)
                {
                    throw new Exception("目标相同，无法移动");
                }
                else
                {
                    //先查找目标单位的新增机构ID
                    string newId = GetNewId(dwbm2);
                    string updateSql = "begin update DWBMB t set t.DWBM='"+newId+"' where t.DWBM='"+dwbm1+"'; Update  DJJCXX t set t.DWBM='" + newId + "' where t.DWBM='" + dwbm1.ToString() + "'; end;";
                    int i = Maticsoft.DBUtility.DbHelperOra.ExecuteSql(updateSql);
                    if (i > 0)
                    {
                        DWBMB dwbmb = new DWBMB();
                        dwbmb.RefreshCache();
                        return "转移成功！";
                    }
                    else
                    {
                        return "没有影响任何数据";
                    }                    
                }
            }            
 
        }
        [AjaxMethod]
        public string GetNewId(string duiId)
        {
            //获取队里最大的站点Id
            object maxid = Maticsoft.DBUtility.DbHelperOra.GetSingle("select max(dwbm) from dwbmb t where t.dwbm like '" + duiId + "%'");

            if (maxid.ToRequestString() == "")
            {
                return duiId + "01";
            }
            else
            {
                string maxnum=maxid.ToString().Substring(maxid.ToString().Length-2, 2);
                if (maxnum.Substring(0,1)== "0")
                {
                    int x = maxnum.ToString().Substring(1, 1).ToInt32() + 1;
                    if (x >= 10)
                    {
                        return duiId + x.ToString();
                    }
                    else
                    {
                        return duiId + "0" + x.ToString();
                    }
                }
                else
                {
                    int x = maxnum.ToInt32() + 1;
                    return duiId + x.ToString();
                }
            }
        }
    }
}